package com.cxx.home.service.impl;

import com.cxx.framework.web.CustomException;
import com.cxx.home.dao.AssetDao;
import com.cxx.home.dto.asset.AssetDto;
import com.cxx.home.entity.Asset;
import com.cxx.home.mapper.AssetMapper;
import com.cxx.home.service.AssetService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssetServiceImpl implements AssetService {
    @Resource
    private AssetDao assetDao;

    @Resource
    private AssetMapper assetMapper;


    @Override
    public Boolean addAsset(AssetDto assetDto) {
        Asset asset = new Asset();
        BeanUtils.copyProperties(assetDto, asset);

        return assetMapper.insert(asset) == 1;
    }

    @Override
    public Boolean updateAsset(Long id, AssetDto assetDto) {
        if (assetMapper.selectById(id) == null) {
            throw new CustomException("该资产不存在");
        }

        Asset asset = new Asset();
        BeanUtils.copyProperties(assetDto, asset);
        asset.setId(id);
        return assetMapper.updateById(asset) == 1;
    }

    @Override
    public Boolean deleteAsset(Long id) {
        if (assetMapper.selectById(id) == null) {
            throw new CustomException("该资产不存在");
        }

        return assetMapper.deleteById(id) == 1;
    }

    @Override
    public List<AssetDto> queryAsset(String category) {
        return assetDao.queryAsset(category);
    }

    @Override
    public List<String> queryAssetCategory() {
        return assetDao.queryAssetCategory();
    }
}
